package com.ysd.lis.service.self.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.LabRptGroup;
import com.ysd.lis.entity.slef.SelfMachineType;
import com.ysd.lis.entity.sta.StaFeeItem;
import com.ysd.lis.mapper.self.SelfMachineTypeMapper;
import com.ysd.lis.request.FeeItemPageParam;
import com.ysd.lis.service.self.SelfMachineTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wh
 * @since 2025-05-12
 */
@Service
public class SelfMachineTypeServiceImpl extends ServiceImpl<SelfMachineTypeMapper, SelfMachineType> implements SelfMachineTypeService {

    @Autowired
    SelfMachineTypeMapper machineTypeMapper;
    @Override
    public Result findMachineTypeList(FeeItemPageParam feeItemPageParam) {

        LambdaQueryWrapper<SelfMachineType> queryWrapper=new LambdaQueryWrapper<>();

        String keywords = feeItemPageParam.getKeyWords();
        if (StrUtil.isNotEmpty(keywords)) {
            queryWrapper.and(wrapper -> wrapper.like(SelfMachineType::getTitleCode, keywords).or().like(SelfMachineType::getTitleName, keywords));
        }
        queryWrapper.eq(SelfMachineType::getDelFlag,0);

        List<SelfMachineType> selfMachineTypes = machineTypeMapper.selectList(queryWrapper);
        return Result.succ(selfMachineTypes);
    }

    @Override
    public Result savaOrUpdateMachineType(SelfMachineType machineType) {
        boolean b = this.saveOrUpdate(machineType);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }

    @Override
    public Result deleteMachineType(List<SelfMachineType> machineTypeList) {
        List<String> ids = machineTypeList.stream().map(SelfMachineType::getId).filter(StringUtils::isNotBlank).collect(Collectors.toList());

        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail("删除失败");
        }    }
}
